<?php

class ColumnController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 * GET /column
	 *
	 * @return Response
	 */
	public function index()
	{
		$columns = Column::all();			//全部栏目
		return View::make('cms.column.index')->with('columns', $columns);
	}

	//增加栏目字段
	public function add(){
		$column_id = Input::get('id');
		$field_name = Input::get('field_name');
		$display_name = Input::get('display_name');
		$field_type = Input::get('field_type');
		ColumnService::fild($column_id,  $field_name, $display_name, $field_type);
	}

	/**
	 * Show the form for creating a new resource.
	 * GET /column/create
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('add', 'column_add');
	}

	/**
	 * Store a newly created resource in storage.
	 * POST /column
	 *
	 * @return Response
	 */
	public function store()
	{
		$column_name = Input::get('column_name');		//页面显示字段
		$table_name = Input::get('table_name');			//数据库对应表名
		$fields = Input::get('fields');					//字段
		$field_type = Input::get('field_type');
		$display_name = Input::get('display_name');
		if($column_name != '' && $table_name != '' && $fields != ''){
			$insertId = ColumnService::add($column_name, $table_name);

			ColumnService::create($table_name, $fields, $field_type, $insertId, $display_name);
		}
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit()
	{
		$id = $_GET['id'];
		$column = Column::where('id', $id)->first();
		$fields = Field::where('id', $id)->get();
		$content = DB::table($column['table_name'])->get();
		return View::make('edit', 'column_edit')->with(array(
						'content' => $content, 
						'table_name' => $column['table_name'],
						'fields' => $fields
						));
		
	}

	//删除栏目字段
	public function delete(){
		$table_name = Input::get('table_name');
		$field_name = Input::get('field_name');		//栏目字段名
		$id = Input::get('id');						//字段表内字段id
		$field = Field::find($id);
		$field->delete();
		Schema::table($table_name, function($table) use ($field_name){
			$table->dropColumn($field_name);
			return "suc";
		});
	}

	/**
	 * 只能修改表字段在页面的显示名
	 * 
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update()
	{
		$display_name = Input::get('display_name');
		$id = Input::get('id');						//字段表内字段id
		$field = Field::find($id);
		$field->display_name= $display_name;
		$field->save();
		return "update suc";
	}

	/**
	 * Remove the specified resource from storage.
	 * DELETE /column/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy()
	{
		$id = $_GET['id'];
		Column::delete($id);
		return Redirect::to('column.index');
	}

}